package com.easy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.easy.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {

//
//        public static void main1(String[] args) throws AlipayApiException {
//            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtt4zbNWinILU9PkfNpGwg0PALbmkZaNnsI6eYK4XbY6wxn3NPqfGgg3NYwErpslfDNEifGREoFx0GiA1D/IvRqGSSj/S+yHqNvO+7wBqgi5w6T8hQKkiSItgz9IS5rmAWfHL5GlT2Ao+DKalQJ+J4qGDNNeepZdcmnKMPc/B+NbQ8F8/apjG0jZPhDn7VmbUTcotGVWb+X/zsn+6xrx/U3ANyAfMkTib52V5ARlCKXEx5NWqEs5vBOCRQnzf0McC6xHR3XLLnYWbU9GtLb+4Ff8GTMXT5Dl4n/nOsfMx6Emg+lII9arppwjOUG7CHdUKTHEs7l7sr0llkSOnMQpYHAgMBAAECggEBAKWmqBzIezkTiYATCalhJvLMCnr8GCBwyY3SGhiTOBuSmjrfj7fhmFW1w6JYC5d5xxKPosiDwMZ38+esiOnRwwp0ByNzPrFGvwa1qe1hLIL3XkyEk8FN8NHZhE1W19GwJFSM6536rBXDL1KlwZx7zAu+lEBbLMNuJ5HcP+mWZzgRLvXUCyxf4cJuOZwX6MhunXDaAnNlB6i8HT+OI0yTvhvQYm33U0EHRnwf3fDIaR7XSW4zznT7uE5OlLQcsnOxed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Y26APjBGOxFj5Ng1TuBatl1W72p++h0Q6gPPp2le2kulqapdqYnPpIRFHM712Y9er3ksB3NMNnxGCXjGUSMdVWU9FI9ogBXTJxM3XIhCIidrbTofNIJ3WVmTc5ZyiICu19Fq3vW37KwGahLJgWQD+YU8IQfVwA2TzR6AplsIKgSXlTOO5mV9BCLcsHAUd1cLUSospCJPYMyphjfd3ZIgUrv/WgCCfF8rMjenp1HFe7sFyc3hZyaw66wAFPX1aPRB/h6VomglsLM4uUsimvcw1fFdU2J3Ovva3GB7hwIDAQAB";
//            AlipayConfig alipayConfig = new AlipayConfig();
//            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
//            alipayConfig.setAppId("9021000140673885");
//            alipayConfig.setPrivateKey(privateKey);
//            alipayConfig.setFormat("json");
//            alipayConfig.setAlipayPublicKey(alipayPublicKey);
//            alipayConfig.setCharset("UTF-8");
//            alipayConfig.setSignType("RSA2");
//            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
//            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
//            model.setOutTradeNo("20150320010101001");
//            model.setTotalAmount("88.88");
//            model.setSubject("Iphone6 16G");
//            request.setBizModel(model);
//            AlipayTradePrecreateResponse response = alipayClient.execute(request);
//            System.out.println(response.getBody());
//            if (response.isSuccess()) {
//                System.out.println("调用成功");
//            } else {
//                System.out.println("调用失败");
//                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//                // System.out.println(diagnosisUrl);
//            }
//        }

        public static String payment(Order order) throws AlipayApiException {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

            // 构造请求参数以调用接口
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

//            // 设置商户订单号
            //model.setOutTradeNo("20150320010101001");
            model.setOutTradeNo(order.getCode());
//
//            // 设置订单总金额
            //model.setTotalAmount("88.88");
            model.setTotalAmount(order.getMoney().toString());
//
//            // 设置订单标题
            model.setSubject("超级宇宙麻辣鸭脖商城");
//
//            // 设置产品码
            model.setProductCode("FACE_TO_FACE_PAYMENT");

            // 设置卖家支付宝用户ID
            model.setSellerId("2088721044641395");

            // 设置订单附加信息
//            model.setBody("Iphone6 16G");
//
//            // 设置订单包含的商品列表信息
//            List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//
//            GoodsDetail goodsDetail0 = new GoodsDetail();
//            goodsDetail0.setGoodsName("ipad");
//            goodsDetail0.setQuantity(1L);
//            goodsDetail0.setPrice("2000");
//            goodsDetail0.setGoodsId("apple-01");
//            goodsDetail0.setGoodsCategory("34543238");
//            goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//            goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//            goodsDetail.add(goodsDetail0);
//            model.setGoodsDetail(goodsDetail);

//            // 设置业务扩展参数
//            ExtendParams extendParams = new ExtendParams();
//            extendParams.setSysServiceProviderId("2088511833207846");
//            model.setExtendParams(extendParams);

            // 设置商户传入业务信息
//            BusinessParams businessParams = new BusinessParams();
//            businessParams.setMcCreateTradeIp("127.0.0.1");
//            model.setBusinessParams(businessParams);

            // 设置可打折金额
//            model.setDiscountableAmount("80.00");

            // 设置商户门店编号
//            model.setStoreId("NJ_001");

            // 设置商户操作员编号
//            model.setOperatorId("yx_001");

            // 设置商户机具终端编号
//            model.setTerminalId("NJ_T_001");

            // 设置商户的原始订单号
//            model.setMerchantOrderNo("20161008001");

            request.setBizModel(model);
//            request.setNotifyUrl("");
            // 第三方代调用模式下请设置app_auth_token
            // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");


            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            System.out.println(response);
            System.out.println(response.getBody());


            if (response.isSuccess()) {
                String body=response.getBody();
                JSONObject obj=JSON.parseObject(body);
                obj= obj.getJSONObject("alipay_trade_precreate_response");
                String qr_code=obj.getString("qr_code");
                System.out.println(qr_code);
                System.out.println("调用成功");
                return qr_code;
            } else {
                System.out.println("调用失败");
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                 String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                 System.out.println(diagnosisUrl);
                 throw new RuntimeException();
            }
        }

        private static AlipayConfig getAlipayConfig() {
            String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtt4zbNWinILU9PkfNpGwg0PALbmkZaNnsI6eYK4XbY6wxn3NPqfGgg3NYwErpslfDNEifGREoFx0GiA1D/IvRqGSSj/S+yHqNvO+7wBqgi5w6T8hQKkiSItgz9IS5rmAWfHL5GlT2Ao+DKalQJ+J4qGDNNeepZdcmnKMPc/B+NbQ8F8/apjG0jZPhDn7VmbUTcotGVWb+X/zsn+6xrx/U3ANyAfMkTib52V5ARlCKXEx5NWqEs5vBOCRQnzf0McC6xHR3XLLnYWbU9GtLb+4Ff8GTMXT5Dl4n/nOsfMx6Emg+lII9arppwjOUG7CHdUKTHEs7l7sr0llkSOnMQpYHAgMBAAECggEBAKWmqBzIezkTiYATCalhJvLMCnr8GCBwyY3SGhiTOBuSmjrfj7fhmFW1w6JYC5d5xxKPosiDwMZ38+esiOnRwwp0ByNzPrFGvwa1qe1hLIL3XkyEk8FN8NHZhE1W19GwJFSM6536rBXDL1KlwZx7zAu+lEBbLMNuJ5HcP+mWZzgRLvXUCyxf4cJuOZwX6MhunXDaAnNlB6i8HT+OI0yTvhvQYm33U0EHRnwf3fDIaR7XSW4zznT7uE5OlLQcsnOxed3sP0MhYxfcUGzwzRKDMHVy76aBMJlVLEnZi/I2pv+mtRrEFEMfXx+xNgcXdWf3uAkk5gTez0zslCHpxmMzHRECgYEA4SpQpTLnQ9sbIcT/LuEK+UFTBCdhJCJw9cAa3UX3Qfc569wH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QD+YU8IQfVwA2TzR6AplsIKgSXlTOO5mV9BCLcsHAUd1cLUSospCJPYMyphjfd3ZIgUrv/WgCCfF8rMjenp1HFe7sFyc3hZyaw66wAFPX1aPRB/h6VomglsLM4uUsimvcw1fFdU2J3Ovva3GB7hwIDAQAB";
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
            alipayConfig.setAppId("9021000140673885");
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setFormat("json");
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");
            return alipayConfig;
        }
    }

